<?php

namespace app\admin\controller;

use app\model\Supplier as SupplierModel;
use app\model\SupplierCate;
use app\model\SupplierCate as SupplierCateModel;

class SupplierController extends Base
{
    public function index()
    {
        if($this->request->isPost()){
            $page = $this->request->post('page', 1, 'intval');
            $limit = $this->request->post('limit', 20, 'intval');
            $keyword = $this->request->post('keyword', '', 'serach_in');
            $cate = $this->request->post('cate', '', 'serach_in');
            $offset = ($page - 1) * $limit;
            $map =[ [
                'status', '=', 1
            ]];
            if(!empty($cate)){
                $map[] = ['cate','=',$cate];
            }


            if(!empty($keyword)){
                $map[] = ['title|tel|addr|contacts','like', "%$keyword%"];
            }
            $item = SupplierModel::where($map)->limit($offset, $limit)->order('id desc')->select();
            $count = SupplierModel::where($map)->count();
            $SupplierCateModel = new SupplierCateModel();

            foreach ($item as $k => $v){
                $item[$k]['cate_name'] = $SupplierCateModel->where(['id'=>$v['cate']])->value('title');

            }

            return $this->json([
                'data' => [
                    'data' => $item,
                    'count' => $count
                ]
            ]);
        }

    }


    public function update()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            if(isset($data['id']) && $data['id'] > 0){
                $state = SupplierModel::where(['id'=>$data['id']])->update($data);
            }else{
                $state = SupplierModel::create($data);
            }
            if($state !== false){
                return $this->json(['msg'=>'提交成功']);
            }else{
                return $this->json(['msg'=>'提交失败', 'code'=>400]);
            }
        }

        return $this->json([]);
    }


    public function delete()
    {
        if($this->request->isPost()){
            $id = $this->request->post('id');
            $state = SupplierModel::where('id', 'in', $id)->update(['status'=>0]);
            if($state !== false){
                return $this->json(['msg'=>'删除成功']);
            }else{
                return $this->json(['msg'=>'删除失败', 'code'=>400]);
            }
        }

    }

    public function cate()
    {
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 10);
        $where = [
            'status' => 1,
        ];
        $offset = ($page - 1) * $limit;
        $SupplierCateModel = new SupplierCateModel();
        $item = $SupplierCateModel->where($where)->limit($offset, $limit)->order('id desc')->select();
        $count = $SupplierCateModel->where($where)->count();
        foreach ($item as $k=>$v){
            $item[$k]['key'] = $v['title'];
            $item[$k]['val'] = $v['id'];
        }
        return $this->json(['code'=>2000, 'msg'=>'获取成功', 'data'=>['item'=>$item, 'count'=>$count]]);
    }

    public function cate_edit()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            if(isset($data['id']) && $data['id'] > 0){
                $state = SupplierCateModel::where(['id'=>$data['id']])->update($data);
            }else{
                $state = SupplierCateModel::create($data);
            }
            if($state !== false){
                return $this->json(['msg'=>'提交成功']);
            }else{
                return $this->json(['msg'=>'提交失败', 'code'=>400]);
            }
        }
        return $this->json([]);
    }

    public function cate_delete()
    {
        if($this->request->isPost()){
            $id = $this->request->post('id');
            $state = SupplierCateModel::where('id', 'in', $id)->update(['status'=>0]);
            if($state !== false){
                return $this->json(['msg'=>'删除成功']);
            }else{
                return $this->json(['msg'=>'删除失败', 'code'=>400]);
            }
        }
    }

    public function vaildate()
    {
        if($this->request->isPost()){
            $id = $this->request->post('id');
            $title = $this->request->post('title');
            $SupplierModel = new SupplierModel();
           if($id > 0){
               $where = [
                   'id' => ['neq', $id],
                   'title' => $title
               ];
               $state = $SupplierModel->where($where)->find();
           }else{
               $state = $SupplierModel->where(['title'=>$title])->find();
           }
           if($state){
               return $this->json(['msg'=>'名称已存在', 'code'=>400]);
           }else{
               return $this->json(['msg'=>'名称可用']);
           }
        }
    }



}